package com.qianf.qfoffice.controller;

import com.github.pagehelper.Page;
import com.qianf.qfoffice.entity.ResponseData;
import com.qianf.qfoffice.entity.User;
import com.qianf.qfoffice.entity.Week;
import com.qianf.qfoffice.service.WeekService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class WeekController {

    @Autowired
    private WeekService weekService;


    @RequestMapping("/week/queryWeek.do")
    @ResponseBody
    public Map<String,Object> queryWeek(Integer page, Integer limit) {

        HashMap<String, Object> hashMap = new HashMap<>();

        List<Week> allWeek = weekService.findAllWeek(page, limit);

        long total = ((Page)allWeek).getTotal();

        hashMap.put("code",0);
        hashMap.put("msg","");
        hashMap.put("count",total);
        hashMap.put("data",allWeek);

        return hashMap;

    }


    @RequestMapping("/week/add.do")
    @ResponseBody
    public ResponseData add (String title, String content, HttpSession session) {

        ResponseData<Object> responseData = new ResponseData<>();

        Week week = new Week();
        User user = (User) session.getAttribute("user");
        week.setTitle(title);
        week.setContent(content);
        week.setUno(user.getNo());

        int i = weekService.addWeek(week);

        if (i > 0 ) {

            responseData.setMsg("添加成功");
            return responseData;
        } else {
            responseData.setMsg("添加失败");
            return responseData;
        }

    }


    @RequestMapping("/week/update.do")
    @ResponseBody
    public void updateWeek (int id, String title, String content, int status, Date createTime, String uno) {

        Week week = new Week();

        week.setId(id);
        week.setTitle(title);
        week.setContent(content);
        week.setCreateTime(createTime);
        week.setUno(uno);

        int i = weekService.updateWeekById(week);


    }


    @RequestMapping("/week/deleteById.do")
    @ResponseBody
    public ResponseData deleteWekById (int id) {

        ResponseData<Object> responseData = new ResponseData<>();

        int i = weekService.deleteWeekById(id);

        if (i > 0 ) {

            responseData.setMsg("删除成功");
            return responseData;
        } else {
            responseData.setMsg("删除失败");
            return responseData;
        }

    }
}
